<?php
/*Ballot class and wordCloud class test site page 3.   	*/
/*This page shows the wordCloud and wordCloudCombo class*/
/*functionality.                                       	*/
/*Takes in checked boxes from previous page, creating 	*/
/*wordClouds based on the names checked.               	*/
/*							*/
/* If user clicked the "Search Clouds" button on p.2	*/
/* this page create wordClouds using words scraped from */
/* Yahoo! search and news search using the two classes	*/
/* yahooNewsSearch and yahooWebSearch.			*/
/* If user clicked the "Issue Clouds" button, wordClouds*/
/* will be created of the issue words in the GLOBALS.php*/
/* ISSUE_LIST array. The size of these words is based on*/
/* the number of hits returned from a yahooWebSearch for*/
/* the issue the candidate's name.                      */
/*							*/
/* Creates one wordcloud for each candidate checked and	*/
/* 	one wordCloudCombo combining the word clouds of	*/
/*	all candidates checked.                        	*/
/* Initially it was intended to allow the user to select*/
/* candidates from multiple offices and to make separate*/
/* tables and separate wordCloudCombos for each office. */
/* This work was not completed and only one Combo cloud */
/* will be made including all checked candidates.       */
/* Due to the slowness of getting the information, it   */
/* probably is infeasible to try to make that many any  */
/* way and in a production environment a limit will be  */
/* needed for the number of candidates a user can choose*/
/********************************************************/
     session_start();
require_once("../lib/Ballot/Ballot.php");
include_once("smarty.inc.php");
include_once("../lib/etc/functions.php");
include_once("GLOBALS.php");

    require_once("../lib/YahooStuff/bossWebSearch.class.php");
    require_once("../lib/YahooStuff/bossNewsSearch.class.php");
    require_once("../lib/WordCloud/classes/wordcloud.class.php");
    require_once("../lib/WordCloud/classes/wordCloudCombo.class.php");
//makes it so user can come back here to enter zip+4 and zipcode is remembered
//session_cache_limiter('private_no_expire');
//flag to erase session data
$finished = true;

$officeArray = array();
$measureArray = array();
$candNames = array();
$myClouds = array();
$myColors = $COLORS;
shuffle($myColors);

$comboCloud = new wordCloudCombo();
    $pattern = '/[\.\-]/';
//get checkbox info
foreach($_POST as $key => $value)
{
     $info = explode('-',$key);

     //make an array of the checked offices/candidates
     if(!strcmp($info[0], "office"))
     {
	  $oname = preg_replace('/[\_\-]/', ' ', strtolower($info[1]));
	  $officeArray[$oname][] = preg_replace('/[\_\-]/', ' ', strtolower($info[3])).' '.preg_replace('/[\_\-]/', ' ', strtolower($info[4]));
     }
     //clouds not implemented for measures at this time
     elseif( !strcmp($info[0], "measure"))
     {
	  $measureArray[] = preg_replace('/[\_\-]/', ' ', strtolower($info[1]));

     }


}
    $searchClouds = false;

    if(!strcmp($_POST['Submit'], "Search Clouds"))
    {
	 $searchClouds = true;
    }
//now do Yahoo! searching for issues

    if($searchClouds)
    {



    //make a wordCloud object for each office, each candidate and each measure
    //have to do websearches for each
    foreach($officeArray as $ofc => $candList)
    {
	  $i = 0;

	 foreach($candList as $c)
	 {
	      $i = $i % 12;//for the colors (there's 12 of them)
	      //reset these each candidate
	       $cloud = new wordCloud();
	       $webSearch = new bossWebSearch();
	       $newsSearch = new bossNewsSearch();
	       $searchString = NULL;
	       $exclude = array();
	       $isOK = true;
	       //set up strings in correct formats
	       $webSearch->addTerm(preg_replace( '/[\ ]/', '%20',$ofc));
	       $newsSearch->addTerm(preg_replace( '/[\ ]/', '%20',$ofc));
	       $webSearch->addTerm(preg_replace( '/[\ ]/', '%20',$c));
	       $newsSearch->addTerm(preg_replace( '/[\ ]/', '%20',$c));
	       $searchString .= preg_replace( '/[\ ]/', '+',$ofc).'+';
	       $searchString .= preg_replace( '/[\ ]/', '+',$c).'+';
	       //setup exclusion of terms from word cloud
	       $pieces = explode(" ",$ofc);
	       foreach($pieces as $p){
		    $exclude[] =strtolower(trim($p));
	       }
	       $pieces = explode(" ",$c);
	       foreach($pieces as $p)
	       {
		    $exclude[] = strtolower(trim($p));
	       }
	       $pieces = explode(" ",$_SESSION['state']);
	       foreach($pieces as $p)
	       {
		    $exclude[] = strtolower(trim($p));
	       }
	       $exclude[] = strtolower(trim($_SESSION['stateAbbr']));


	  try{
	       $webSearch->search();
	       $keywords = $webSearch->getKeywords($STOP_WORDS);
	  }
	  catch(NeedSearchTermsException $nste)
	  {
	      // echo $nste;
	       $isOK = false;
	  }
	  catch(NoWebResultsException $nwre)
	  {
	     //  echo $nwre;
	       $isOK = false;
	  }

	  if($isOK){


	       //add found words to the cloud
	       foreach($keywords as $word)
	       {
		    if(strlen($word) > 2){
			 $cloud->addWord($word,'http://search.yahoo.com/search?p='.$searchString , $myColors[$i]);
		    }
	       }


	 }
	  //do newssearch
	  $isOK = true;
	  try{
	       $newsSearch->search();
	       $resultArray = $newsSearch->getKeyWordArray($STOP_WORDS);
	  }
	  catch(NeedNewsTermsException $nnte)
	  {
	      // echo $nnte;
	       $isOK = false;
	  }
	  catch(NoNewsResultsException $nnre)
	  {
	      // echo $nnre;
	       $isOK = false;
	  }
	  if($isOK)
	  {

	       //add found words to the cloud
	       foreach($resultArray as $word)
	       {
		    if(strlen($word) > 2){
			 //echo 'Added color='.$colorArray[$i].':<br />';
			 $cloud->addWord($word,'http://search.yahoo.com/search?p='.$searchString , $myColors[$i]);
		    }
	       }



	  }
	  //take out excluded search terms

	  foreach($exclude as $e)
	  {
	       $cloud->removeWord($e);
	       //add all exclusions to comboCloud??
	       $comboCloud->removeWord($e);
	  }
	  //  $cloud->setCountLimit(1);
	  $cloud->setWordLimit(50);

	  //sends raw array rather than limited fixed one
	  //key 'name' will be candidate's name
	  $comboCloud->addCloud($cloud->wordsArray, $c);

	  $myClouds[$c] = $cloud->getCloud();


	  $i++;//increase color counter
     }
    }

    }else{//do issue clouds


       //make a wordCloud object for each office, each candidate and each measure
    //have to do websearches for each
    foreach($officeArray as $ofc => $candList)
    {
	  $i = 0;

	 foreach($candList as $c)
	 {
	       $cloud = new wordCloud();
	       $exclude = array();
	       $i = $i % 12;//for the colors (there's 12 of them)
	//this cloud will include phrases with spaces in them
	       $exclude[] = $ofc;
	       $pieces = explode(" ",$ofc);
	       foreach($pieces as $p){
		    $exclude[] =strtolower(trim($p));
	       }
	       $exclude[] = $c;
	       $pieces = explode(" ",$c);
	       foreach($pieces as $p)
	       {
		    $exclude[] = strtolower(trim($p));
	       }

	  //do a search for each term in the issue list
	  //add the returned search counts to count/size in wordcloud
	  foreach($ISSUE_LIST as $heading => $issues)
	  {
	       //first do the issue array headings
	       $webSearch = new bossWebSearch();
	       $newsSearch = new bossNewsSearch();
	       $searchString = NULL;
		    $isOK = true;
		    //set up strings in correct formats
		    $webSearch->addTerm(preg_replace( '/[\ ]/', '%20',$ofc));
		    $newsSearch->addTerm(preg_replace( '/[\ ]/', '%20',$ofc));
		    $webSearch->addTerm(preg_replace( '/[\ ]/', '%20',$c));
		    $newsSearch->addTerm(preg_replace( '/[\ ]/', '%20',$c));
		    $webSearch->addTerm(preg_replace( '/[\ ]/', '%20',$heading));
		    $newsSearch->addTerm(preg_replace( '/[\ ]/', '%20',$heading));
		    $searchString .= preg_replace( '/[\ ]/', '+',$ofc).'+';
		    $searchString .= preg_replace( '/[\ ]/', '+',$c).'+';


		    try{
		    $hits = $webSearch->getHits();

		    }
		    catch(NeedSearchTermsException $nste)
		    {
		    // echo $nste;
			 $isOK = false;
		    }
		    catch(NoWebResultsException $nwre)
		    {
		    //  echo $nwre;
			 $isOK = false;
		    }

		    if($isOK){
		    $cloud->addWord($heading,'http://search.yahoo.com/search?p='.$searchString , $myColors[$i], $hits);

		    }
		    foreach($issues as $issue)
		    {

			 $webSearch = new bossWebSearch();
			 $newsSearch = new bossNewsSearch();
			 $searchString = NULL;
			 $isOK = true;
			 //set up strings in correct formats
			 $webSearch->addTerm(preg_replace( '/[\ ]/', '%20',$ofc));
			 $newsSearch->addTerm(preg_replace( '/[\ ]/', '%20',$ofc));
			 $webSearch->addTerm(preg_replace( '/[\ ]/', '%20',$c));
			 $newsSearch->addTerm(preg_replace( '/[\ ]/', '%20',$c));
			 $webSearch->addTerm(preg_replace( '/[\ ]/', '%20',$issue));
			 $newsSearch->addTerm(preg_replace( '/[\ ]/', '%20',$issue));
			 $searchString .= preg_replace( '/[\ ]/', '+',$ofc).'+';
			 $searchString .= preg_replace( '/[\ ]/', '+',$c).'+';


			 try{
			      $webSearch->search();
			      $hits = $webSearch->getHits();

			 }
			 catch(NeedSearchTermsException $nste)
			 {
			 // echo $nste;
			      $isOK = false;
			 }
			 catch(NoWebResultsException $nwre)
			 {
			 //  echo $nwre;
			      $isOK = false;
			 }

			 if($isOK){
			      $cloud->addWord($issue,'http://search.yahoo.com/search?p='.$searchString , $myColors[$i], $hits);

			 }

		    }



     }
		    		    //take out excluded search terms

		    foreach($exclude as $e)
		    {
			 $cloud->removeWord($e);
			 //add all exclusions to comboCloud??
			 $comboCloud->removeWord($e);
		    }
		    //  $cloud->setCountLimit(1);
		    $cloud->setWordLimit(40);

		    //sends raw array rather than limited fixed one
		    //key 'name' will be candidate's name
		    $comboCloud->addCloud($cloud->wordsArray, $c);
// 		    if($searchClouds){
// 			 $shuffle = true;
// 		    }else{
// 			 $shuffle = false;//want issue clouds in same order
// 		    }
		    $myClouds[$c] = $cloud->getCloud();


		    $i++;//increase color counter
 	       }
	  }
    }

$comboCloud->setWordLimit(40);

$smarty->assign('searchClouds', $searchClouds);

$smarty->assign('clouds', $myClouds);
$smarty->assign('combo', $comboCloud->getCloud());

$smarty->assign('ofcArr', $officeArray);
$smarty->assign('colors', $myColors);

$smarty->display('page3.tpl');

if($finished) killSession();
?>